<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="学号" prop="studentId">
        <el-input v-model="queryParams.studentId" placeholder="请输入学号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="详细专业" prop="detailedMajor">
        <el-input v-model="queryParams.detailedMajor" placeholder="请输入详细专业" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option v-for="dict in string" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="就业意向" prop="employmentIntent">
        <el-select v-model="queryParams.employmentIntent" placeholder="请选择就业意向" clearable>
          <el-option v-for="dict in employment_intent" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="就业状况" prop="employmentStatus">
        <el-select v-model="queryParams.employmentStatus" placeholder="请选择就业状况" clearable>
          <el-option v-for="dict in employment_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="签约单位名称" prop="signedCompany">
        <el-input
          v-model="queryParams.signedCompany"
          placeholder="请输入签约单位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现工作单位名称" prop="currentCompany">
        <el-input
          v-model="queryParams.currentCompany"
          placeholder="请输入现工作单位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否在校" prop="isOnCampus">
        <el-select v-model="queryParams.isOnCampus" placeholder="请选择是否在校" clearable>
          <el-option
            v-for="dict in is_on_campus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item label="就业所在省份" prop="province">
        <el-input v-model="queryParams.province" placeholder="请输入就业所在省份" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['system:EmploymentInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:EmploymentInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:EmploymentInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:EmploymentInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="EmploymentInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="studentId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="详细专业" align="center" prop="detailedMajor" />
      <el-table-column label="性别" align="center" prop="gender">
        <template #default="scope">
          <dict-tag :options="string" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="就业意向" align="center" prop="employmentIntent">
        <template #default="scope">
          <dict-tag :options="employment_intent" :value="scope.row.employmentIntent" />
        </template>
      </el-table-column>
      <el-table-column label="就业状况" align="center" prop="employmentStatus">
        <template #default="scope">
          <dict-tag :options="employment_status" :value="scope.row.employmentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="签约单位名称" align="center" prop="signedCompany" />
      <el-table-column label="现工作单位名称" align="center" prop="currentCompany" />
      <el-table-column label="签约单位变更情况" align="center" prop="companyChange" />
      <el-table-column label="是否在校" align="center" prop="isOnCampus">
        <template #default="scope">
          <dict-tag :options="is_on_campus" :value="scope.row.isOnCampus" />
        </template>
      </el-table-column>


      <el-table-column label="就业所在省份" align="center" prop="province" />


      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:EmploymentInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:EmploymentInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改就业信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="EmploymentInfoRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="学号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="详细专业" prop="detailedMajor">
          <el-input v-model="form.detailedMajor" placeholder="请输入详细专业" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option v-for="dict in string" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="就业意向" prop="employmentIntent">
          <el-select v-model="form.employmentIntent" placeholder="请选择就业意向">
            <el-option v-for="dict in employment_intent" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="就业状况" prop="employmentStatus">
          <el-select v-model="form.employmentStatus" placeholder="请选择就业状况">
            <el-option v-for="dict in employment_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签约单位名称" prop="signedCompany">
          <el-input v-model="form.signedCompany" placeholder="请输入签约单位名称" />
        </el-form-item>
        <el-form-item label="现工作单位名称" prop="currentCompany">
          <el-input v-model="form.currentCompany" placeholder="请输入现工作单位名称" />
        </el-form-item>
        <el-form-item label="签约单位变更情况" prop="companyChange">
          <el-input v-model="form.companyChange" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否在校" prop="isOnCampus">
          <el-select v-model="form.isOnCampus" placeholder="请选择是否在校">
            <el-option v-for="dict in is_on_campus" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所在省份" prop="province">
          <el-select v-model="form.province" placeholder="请选择所在省份">
            <el-option v-for="item in provinces" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable v-model="form.createdAt" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="EmploymentInfo">
import { listEmploymentInfo, getEmploymentInfo, delEmploymentInfo, addEmploymentInfo, updateEmploymentInfo, countByEmploymentStatus, countByEmploymentIntent, countByEmploymentProvince, getEmploymentStatsAndIntent, getProvinceStats } from "@/api/system/EmploymentInfo";
import useUserStore from '@/store/modules/user'

const { proxy } = getCurrentInstance();
const { is_on_campus, employment_status, employment_intent, string } = proxy.useDict('is_on_campus', 'employment_status', 'employment_intent', 'string');

const EmploymentInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const userStore = useUserStore();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    studentId: null,
    name: null,
    detailedMajor: null,
    gender: null,
    employmentIntent: null,
    employmentStatus: null,
    signedCompany: null,
    currentCompany: null,
    companyChange: null,
    isOnCampus: null,
  },
  rules: {
    studentId: [
      { required: true, message: "学号不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    gender: [
      { required: true, message: "性别不能为空", trigger: "change" }
    ],
  }
});

const provinces = ref([
  { value: '北京市', label: '北京市' },
  { value: '天津市', label: '天津市' },
  { value: '河北省', label: '河北省' },
  { value: '山西省', label: '山西省' },
  { value: '内蒙古自治区', label: '内蒙古自治区' },
  { value: '辽宁省', label: '辽宁省' },
  { value: '吉林省', label: '吉林省' },
  { value: '黑龙江省', label: '黑龙江省' },
  { value: '上海市', label: '上海市' },
  { value: '江苏省', label: '江苏省' },
  { value: '浙江省', label: '浙江省' },
  { value: '安徽省', label: '安徽省' },
  { value: '福建省', label: '福建省' },
  { value: '江西省', label: '江西省' },
  { value: '山东省', label: '山东省' },
  { value: '河南省', label: '河南省' },
  { value: '湖北省', label: '湖北省' },
  { value: '湖南省', label: '湖南省' },
  { value: '广东省', label: '广东省' },
  { value: '广西壮族自治区', label: '广西自治区' },
  { value: '海南省', label: '海南省' },
  { value: '重庆市', label: '重庆市' },
  { value: '四川省', label: '四川省' },
  { value: '贵州省', label: '贵州省' },
  { value: '云南省', label: '云南省' },
  { value: '西藏自治区', label: '西藏自治区' },
  { value: '陕西省', label: '陕西省' },
  { value: '甘肃省', label: '甘肃省' },
  { value: '青海省', label: '青海省' },
  { value: '宁夏回族自治区', label: '宁夏自治区' },
  { value: '新疆维吾尔自治区', label: '新疆自治区' },
  { value: '台湾省', label: '台湾省' },
  { value: '香港特别行政区', label: '香港特别行政区' },
  { value: '澳门特别行政区', label: '澳门特别行政区' }
]);

const { queryParams, form, rules } = toRefs(data);
const uid = userStore.id;

/** 查询就业信息列表 */
function getList() {
  loading.value = true;
  listEmploymentInfo(queryParams.value,uid).then(response => {
    EmploymentInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/**
 * 统计就业接口
 */
function getCountByEmploymentStatus() {
  countByEmploymentStatus().then(response => {
    console.log(response);
  });
}

function getCountByEmploymentIntent() {
  countByEmploymentIntent().then(response => {
    console.log(response);
  });
}

function getCountByEmploymentProvince() {
  countByEmploymentProvince().then(response => {
    console.log(response);
  });
}

getEmploymentStatsAndIntent().then(response => {
  getEmploymentStatsAndIntent().then(response => {
    console.log(response);
  });
})

getProvinceStats().then(response => {
  console.log(response);
})



// 取消按钮

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    studentId: null,
    name: null,
    detailedMajor: null,
    gender: null,
    employmentIntent: null,
    employmentStatus: null,
    signedCompany: null,
    currentCompany: null,
    companyChange: null,
    isOnCampus: null,
    province: null,
    createdAt: null,
    updatedAt: null
  };
  proxy.resetForm("EmploymentInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加就业信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getEmploymentInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改就业信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["EmploymentInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateEmploymentInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addEmploymentInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除就业信息编号为"' + _ids + '"的数据项？').then(function () {
    return delEmploymentInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hour = String(now.getHours()).padStart(2, '0');
  const minute = String(now.getMinutes()).padStart(2, '0');
  const fileName = `就业信息_${year}${month}${day}_${hour}${minute}.xlsx`;
  proxy.download(`system/EmploymentInfo/export/${uid}`, {
    ...queryParams.value
  }, fileName)
}

getProvinceStats();
getEmploymentStatsAndIntent();
getCountByEmploymentProvince();
getCountByEmploymentIntent();
getCountByEmploymentStatus();
getList();
</script>
